package cn.madis.ymcc.mq.consumer;

import cn.madis.ymcc.dto.PayNotifyDto;
import cn.madis.ymcc.service.ICourseUserLearnService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;


@Component
@RocketMQMessageListener(
        consumerGroup = "alipay-course-consumer",//组名
        topic = "paynotify-topic",
        selectorExpression = "alipay-tags",//tags
        messageModel = MessageModel.BROADCASTING//广播模式
        //集群模式一个消费者只能给到一个消费者

)
public class PaySuccessUpdateCourseConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseUserLearnService courseUserLearnService;


    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        String s = new String(body, StandardCharsets.UTF_8);
        PayNotifyDto aliPayNotifyDto = JSONObject.parseObject(s, PayNotifyDto.class);
        /*添加CourseUserLearn客户购买课程信息*/
        courseUserLearnService.addPayCourse(aliPayNotifyDto);

    }
}

